﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;

namespace Nirvana
{
    /// <summary>
    /// 用于toggle控制对象的显示、隐藏。带canvase的对象则可以带alpha动画
    /// </summary>
    [AddComponentMenu("Nirvana/UI/Control/Toggle Activator")]
    [RequireComponent(typeof(Toggle))]
    public class ToggleActivator : MonoBehaviour
    {
        public enum TransitionMode
        {
            Instant,
            Fade
        }

        [SerializeField]
        private GameObject[] actives;
        [SerializeField]
        private GameObject[] deactives;
        [SerializeField]
        [Tooltip("只在IsOn=true时显示的对象")]
        private GameObject[] activesOnly;
        [Tooltip("只在IsOn=false时隐藏的对象")]
        [SerializeField]
        private GameObject[] deactivesOnly;
        [SerializeField]
        private TransitionMode transitionMode;
        [SerializeField]
        private float transitionTime = 0.1f;
        private Toggle _toggle;

        private void Awake()
        {
            this._toggle = base.GetComponent<Toggle>();
            Assert.IsNotNull<Toggle>(this._toggle);
            this.OnValueChanged(this._toggle.isOn);
            this._toggle.onValueChanged.AddListener(OnValueChanged);
        }
        private void OnEnable()
        {
            this.OnValueChanged(this._toggle.isOn);
        }

        private void OnValueChanged(bool IsOn)
        {
            foreach (var g in this.actives)
            {
                this.SetActive(gameObject, IsOn);
            }
            foreach (var g in this.deactives)
            {
                this.SetActive(g, !IsOn);
            }
            if (IsOn)
            {
                foreach (var g in this.activesOnly)
                {
                    this.SetActive(g, true);
                }
            }
            else
            {
                foreach (var g in this.deactivesOnly)
                {
                    this.SetActive(g, false);
                }
            }
        }

        private void SetActive(GameObject obj, bool IsOn)
        {
            if (this.transitionMode == TransitionMode.Instant)
            {
                obj.SetActive(IsOn);
            }
            else
            {
                CanvasGroup canvas = obj.GetComponent<CanvasGroup>();
                if (canvas != null)
                {
                    obj.SetActive(true);
                    if (obj.activeInHierarchy)
                    {
                        if (IsOn)
                        {
                            base.StartCoroutine(this.ToFade(canvas, 1f, true));
                        }
                        else
                        {
                            base.StartCoroutine(this.ToFade(canvas, 0f, false));
                        }
                    }
                    else
                    {
                        obj.SetActive(IsOn);
                    }
                }
                else
                {
                    obj.SetActive(IsOn);
                }
            }
        }

        private IEnumerator ToFade(CanvasGroup canvas, float alphavalue, bool isActive)
        {
            float num = this.transitionTime;
            float alpha = canvas.alpha;
            while (num > 0f)
            {
                yield return null;
                num -= Time.deltaTime;
                float alpha2 = Mathf.Lerp(alpha, alphavalue, 1f - num / this.transitionTime);
                canvas.alpha = alpha2;
            }
            canvas.gameObject.SetActive(isActive);
            yield break;
        }
    }
}

